$(function () {

    var dept_datagrid = $("#dept_datagrid");
    var dept_dialog = $("#dept_dialog");
    var dept_form = $("#dept_form");


    /*easyui的数据列表组件初始化*/
    dept_datagrid.datagrid({
        title:"员工管理",
        url: "/department/query.do",
        fit: true,
        striped: true,
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        singleSelect: true,
        sortName: 'hireDate',
        sortOrder: 'desc',
        toolbar: "#dept_toolbar",
        onSelect: function (index, row) {
            if (row.state) {
                $("#btn_state").linkbutton({
                    text: "停用"
                })
            } else {
                $("#btn_state").linkbutton({
                    text: "启用"
                })
            }
        },
        columns: [[
            {field: 'id', title: 'id', width: 100, checkbox: true},
            {field: 'name', title: '部门名称', width: 100},
            {field: 'sn', title: '部门编号', width: 100},
            {
                field: 'state', title: '状态', width: 100, formatter: function (value, row, index) {
                    return value ? "正常" : "<span style='color: red'>停用</span>";
                }
            },
        ]]
    })

    /*弹出编辑框的初始化*/
    dept_dialog.dialog({
        width: 270,
        height: 150,
        closed: true,
        buttons: [{
            text: "保存",
            iconCls: "icon-save",
            plain: true,
            handler: function () {
                methodObj["save"]();
            }
        }, {
            text: "取消",
            iconCls: "icon-no",
            plain: true,
            handler: function () {
                methodObj["cancel"]();
            }
        }],
        onClose: function () {
            dept_form.form('clear');
        }
    })

    /*事件对象*/
    var methodObj = {
        reload: function () {
            console.log("A");
            dept_datagrid.datagrid("reload");
        },

        /*新增部门按钮事件*/
        add: function () {
            dept_dialog.dialog('setTitle', "新增部门");
            dept_dialog.dialog('open');
        },

        /*编辑部门按钮事件*/
        edit: function () {
            dept_dialog.dialog('setTitle', "编辑");
            /*c查看是否有选中的部门*/
            var row = dept_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选择一条数据!', 'error');
                return;
            }
            /*数据回显*/
            dept_form.form('load', row);
            /*打开弹出框*/
            dept_dialog.dialog('open');
        },

        /*状态改变事件*/
        changeState: function () {
            var row = dept_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert("温馨提示", "请选择一行数据", "error");
                return;
            }
            $.messager.confirm("温馨提示", '确定要改变状态吗?', function (tr) {
                if(tr){
                    $.post("/department/changeState.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", "操作成功", "question", function () {
                                dept_datagrid.datagrid('reload');
                            });
                        } else {
                            $.messager.alert("温馨提示", "操作失败", "error");
                        }
                    })
                }
            })
        },

        /*表单保存事件*/
        save: function () {
            $.messager.confirm("温馨提示", "确定要保存吗?", function () {
                dept_form.form('submit', {
                    url: "/department/saveOrUpdate.do",
                    success: function (data) {
                        /*easyui的表单响应数据不是json,需要转换*/
                        data = $.parseJSON(data);
                        if (data.success) {
                            $.messager.alert("温馨提示", "操作成功", "info", function () {
                                dept_dialog.dialog('close');
                                dept_datagrid.datagrid('reload');
                            })
                        } else {
                            $.messager.alert("温馨提示", "操作失败", "error")
                        }
                    }
                })
            })
        },

        /*表单取消事件*/
        cancel: function () {
            dept_dialog.dialog('close');
        }
    }

    /*统一绑定事件*/
    $("a[data-method]").click(function () {
        console.log("");
        var method = $(this).data("method");
        methodObj[method]();
    })
})
